setwd("/Users/janeesberg/Dropbox/PhD/RepressedPols/RepressedPols_May2019")
source('RepressedPols_DatasetDev_JOP.R')
iterations=5000

#Purpose of file: replication code for main paper, "Anticipating Dissent: The Repression of Politicians in Pinochet's Chile"

#R version 3.5.3 (2019-03-11) macOS Mojave 10.14.6

##Note: bootstrapping takes several hours to run

############Prep data for regression discontinuity############
LLR_anyrepression_data<-makeLLR(dips73, dips73$anyrepression, dips73$quotient_diff, 0, 'dips73')
LLR_physicalcoercion_data<-makeLLR(dips73, dips73$victim, dips73$quotient_diff, 0, 'dips73')

noasylum_anyrepression_data<-makeLLR(dips73_noasylum, dips73_noasylum$anyrepression, dips73_noasylum$quotient_diff, 0, 'dips73_noasylum')
noasylum_physicalcoercion_data<-makeLLR(dips73_noasylum, dips73_noasylum$victim, dips73_noasylum$quotient_diff, 0, 'dips73_noasylum')

surname_data<-covariateBalanceTestData(dips73, dips73$surname)
gender_data<-covariateBalanceTestData(dips73, dips73$gender)
dynastic_data<-covariateBalanceTestData(dips73, dips73$dynastic)
incumbent_data<-covariateBalanceTestData(dips73, dips73$incumbent_orpastelected)
list_data<-covariateBalanceTestData(dips73, dips73$UP)
usopo_data<-covariateBalanceTestData(dips73, I(dips73$list=='USOPO'))

press_data<-makeLLR(dips73, dips73$CD_presscoverage, dips73$quotient_diff, 0, 'dips73')
press_data2<-makeAlt(dips73, dips73$CD_presscoverage, dips73$quotient_diff, 0, 'dips73', 2)
press_data_victim<-makeAlt(dips73, dips73$CD_presscoverage, dips73$quotient_diff, 0, 'dips73', 2.81)

######################################
#######Figure 1 -- T-tests ###########
######################################

ttest_anyrepression<-ttest_electedvunelected(dips73$anyrepression, dips73)
ttest_anyrepression$type<-'3. anyrepression_all'

ttest_anyrepression_nousopo<-ttest_electedvunelected(dips73_nousopo$anyrepression, dips73_nousopo)
ttest_anyrepression_nousopo$type<-'2. anyrepression_nousopo'

ttest_anyrepression_leftDC<-ttest_electedvunelected(dips73_leftDC_nousopo$anyrepression, dips73_leftDC_nousopo)
ttest_anyrepression_leftDC$type<-'1. anyrepression_left'

anyrepression_ttest_df<-rbind(ttest_anyrepression, ttest_anyrepression_nousopo, ttest_anyrepression_leftDC)
colnames(anyrepression_ttest_df)<-c('elected', 'unelected', 'difference', 'lower', 'upper', 'CI', 'type')


anyrepression_ttest_plot<-ggplot(anyrepression_ttest_df, aes(x=type, y=as.numeric(as.character(difference)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(anyrepression_ttest_df$lower)), 
                             ymax=as.numeric(as.character(anyrepression_ttest_df$upper)))+
  ylim(-10, 30)+scale_linetype_manual(values=c('solid', 'dotted'),
                                      guide = guide_legend(override.aes = list(
                                        linetype = c("solid", "dotted"),
                                        shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  ylab("") +
  xlab("")+scale_x_discrete(labels=c('UP and PDC', 'Excluding USOPO', 'All Candidates'))
#ggsave('anyrepression_ttest_plot.pdf', plot=anyrepression_ttest_plot, height=4, width=5)




ttest_physicalcoercion<-ttest_electedvunelected(dips73$victim, dips73)
ttest_physicalcoercion$type<-'3. victim_all'

ttest_victim_nousopo<-ttest_electedvunelected(dips73_nousopo$victim, dips73_nousopo)
ttest_victim_nousopo$type<-'2. victim_nousopo'

ttest_victim_leftDC<-ttest_electedvunelected(dips73_leftDC_nousopo$victim, dips73_leftDC_nousopo)
ttest_victim_leftDC$type<-'1. victim_left'

victim_ttest_df<-rbind(ttest_physicalcoercion, ttest_victim_nousopo, ttest_victim_leftDC)
colnames(victim_ttest_df)<-c('elected', 'unelected', 'difference', 'lower', 'upper', 'CI', 'type')

physicalcoercion_ttest_plot<-ggplot(victim_ttest_df, aes(x=type, y=as.numeric(as.character(difference)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(victim_ttest_df$lower)), 
                             ymax=as.numeric(as.character(victim_ttest_df$upper)))+
  ylim(-25, 10)+scale_linetype_manual(values=c('solid', 'dotted'),
                                      guide = guide_legend(override.aes = list(
                                        linetype = c("solid", "dotted"),
                                        shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  scale_x_discrete(labels=c('UP and PDC', 'Excluding USOPO', 'All Candidates'))

#ggsave('physicalcoercion_ttest_plot.pdf', plot=physicalcoercion_ttest_plot, height=4, width=5)

####################################################
#########Figure 2 -- Logistic Regression ###########
####################################################

#Overall repression, model 1 -- no controls
ar_logit_noc<-logitCoefficients(dips73, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc))/2

elected_anyrepression_results=as.data.frame(t(matrix(unlist(ar_logit_noc), ncol=functional_iterations)))
elected_anyrepression_reg<-glm(anyrepression~vote_perc, data=dips73, family="binomial")
elected_elected_anyrepression<-format(round(summary(elected_anyrepression_reg)$coefficients[2,1], 2))
#write(elected_elected_anyrepression, "elected_elected_anyrepression.tex")
elected_anyrepression_AIC<-format(round(summary(elected_anyrepression_reg)$aic, 2))
#write(elected_anyrepression_AIC, "elected_anyrepression_AIC.tex")

elected_anyrepression_lower<-format(round(quantile(elected_anyrepression_results$V2,probs=.05), 2))
#write(elected_anyrepression_lower, "elected_anyrepression_lower.tex")
elected_anyrepression_upper<-format(round(quantile(elected_anyrepression_results$V2,probs=.95), 2))
#write(elected_anyrepression_upper, "elected_anyrepression_upper.tex")

elected_constant_anyrepression<-format(round(summary(elected_anyrepression_reg)$coefficients[1,1], 2))
#write(elected_constant_anyrepression, "elected_constant_anyrepression.tex")
elected_constant_anyrepression_lower<-format(round(quantile(elected_anyrepression_results$V1,probs=.05), 2))
#write(elected_constant_anyrepression_lower, "elected_constant_anyrepression_lower.tex")
elected_constant_anyrepression_upper<-format(round(quantile(elected_anyrepression_results$V1,probs=.95), 2))
#write(elected_constant_anyrepression_upper, "elected_constant_anyrepression_upper.tex")

##Overall repression, model 2 --  controls
ar_logit_c<-logitCoefficients(dips73, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_logit_c))/2

electedc_anyrepression_results=as.data.frame(t(matrix(unlist(ar_logit_c), ncol=functional_iterations)))

electedc_anyrepression_reg<-glm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73, family="binomial")
electedc_elected_anyrepression<-format(round(summary(electedc_anyrepression_reg)$coefficients[2,1], 2))
#write(electedc_elected_anyrepression, "electedc_elected_anyrepression.tex")
electedc_anyrepression_AIC<-format(round(summary(electedc_anyrepression_reg)$aic, 2))
#write(electedc_anyrepression_AIC, "electedc_anyrepression_AIC.tex")

electedc_anyrepression_lower<-format(round(quantile(electedc_anyrepression_results$V2,probs=.05), 2))
#write(electedc_anyrepression_lower, "electedc_anyrepression_lower.tex")
electedc_anyrepression_upper<-format(round(quantile(electedc_anyrepression_results$V2,probs=.95), 2))
#write(electedc_anyrepression_upper, "electedc_anyrepression_upper.tex")

electedc_constant_anyrepression<-format(round(summary(electedc_anyrepression_reg)$coefficients[1,1], 2))
#write(electedc_constant_anyrepression, "electedc_constant_anyrepression.tex")
electedc_constant_anyrepression_lower<-format(round(quantile(electedc_anyrepression_results$V1,probs=.05), 2))
#write(electedc_constant_anyrepression_lower, "electedc_constant_anyrepression_lower.tex")
electedc_constant_anyrepression_upper<-format(round(quantile(electedc_anyrepression_results$V1,probs=.95), 2))
#write(electedc_constant_anyrepression_upper, "electedc_constant_anyrepression_upper.tex")

##Overall repression, model 3 -- full controls
ar_logit_pdc<-logitCoefficients(dips73, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_logit_pdc))/2

electedpdc_anyrepression_results=as.data.frame(t(matrix(unlist(ar_logit_pdc), ncol=functional_iterations)))
electedpdc_anyrepression_reg<-glm(anyrepression~vote_perc+factor(list)+factor(agrupacion)+surname+dynastic+incumbent_orpastelected, data=dips73, family="binomial")

electedpdc_elected_anyrepression<-format(round(summary(electedpdc_anyrepression_reg)$coefficients[2,1], 2))
#write(electedpdc_elected_anyrepression, "electedpdc_elected_anyrepression.tex")
electedpdc_anyrepression_AIC<-format(round(summary(electedpdc_anyrepression_reg)$aic, 2))
#write(electedpdc_anyrepression_AIC, "electedpdc_anyrepression_AIC.tex")

electedpdc_anyrepression_lower<-format(round(quantile(electedpdc_anyrepression_results$V2,probs=.05), 2))
#write(electedpdc_anyrepression_lower, "electedpdc_anyrepression_lower.tex")
electedpdc_anyrepression_upper<-format(round(quantile(electedpdc_anyrepression_results$V2,probs=.95), 2))
#write(electedpdc_anyrepression_upper, "electedpdc_anyrepression_upper.tex")

electedpdc_constant_anyrepression<-format(round(summary(electedpdc_anyrepression_reg)$coefficients[1,1], 2))
#write(electedpdc_constant_anyrepression, "electedpdc_constant_anyrepression.tex")
electedpdc_constant_anyrepression_lower<-format(round(quantile(electedpdc_anyrepression_results$V1,probs=.05), 2))
#write(electedpdc_constant_anyrepression_lower, "electedpdc_constant_anyrepression_lower.tex")
electedpdc_constant_anyrepression_upper<-format(round(quantile(electedpdc_anyrepression_results$V1,probs=.95), 2))
#write(electedpdc_constant_anyrepression_upper, "electedpdc_constant_anyrepression_upper.tex")

###Physical coercion, model 1 -- no controls
pc_logit_noc<-logitCoefficients(dips73, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc))/2

elected_results=as.data.frame(t(matrix(unlist(pc_logit_noc), ncol=functional_iterations)))

elected_reg<-glm(victim~factor(elected), data=dips73, family="binomial")
elected_elected<-format(round(summary(elected_reg)$coefficients[2,1], 2))
elected_AIC<-format(round(summary(elected_reg)$aic, 2))
elected_obs<-dim(dips73)[1]
#write(elected_elected, "elected_elected.tex")
#write(elected_AIC, "elected_AIC.tex")
#write(elected_obs, "elected_obs.tex")

elected_lower<-format(round(quantile(elected_results$V2,probs=.05), 2))
#write(elected_lower, "elected_lower.tex")
elected_upper<-format(round(quantile(elected_results$V2,probs=.95), 2))
#write(elected_upper, "elected_upper.tex")

elected_constant<-format(round(summary(elected_reg)$coefficients[1,1], 2))
#write(elected_constant, "elected_constant.tex")

elected_constant_lower<-format(round(quantile(elected_results$V1,probs=.05), 2))
#write(elected_constant_lower, "elected_constant_lower.tex")
elected_constant_upper<-format(round(quantile(elected_results$V1,probs=.95), 2))
#write(elected_constant_upper, "elected_constant_upper.tex")

##Physical coercion, model 2 -- controls
pc_logit_c<-logitCoefficients(dips73, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(pc_logit_c))/2

electedc_results=as.data.frame(t(matrix(unlist(pc_logit_c), ncol=functional_iterations)))

electedc_glm<-glm(victim~factor(elected)+surname+incumbent_orpastelected+dynastic, data=dips73, family="binomial")
electedc_elected<-format(round(summary(electedc_glm)$coefficients[2,1], 2))
#write(electedc_elected, "electedc_elected.tex")

electedc_AIC<-format(round(summary(electedc_glm)$aic, 2))
#write(electedc_AIC, "electedc_AIC.tex")
electedc_lower<-format(round(quantile(electedc_results$V2,probs=.05), 2))
#write(electedc_lower, "electedc_lower.tex")
electedc_upper<-format(round(quantile(electedc_results$V2,probs=.95), 2))
#write(electedc_upper, "electedc_upper.tex")

electedc_constant<-format(round(summary(electedc_glm)$coefficients[1,1], 2))
#write(electedc_constant, "electedc_constant.tex")
electedc_constant_lower<-format(round(quantile(electedc_results$V1,probs=.05), 2))
#write(electedc_constant_lower, "electedc_constant_lower.tex")
electedc_constant_upper<-format(round(quantile(electedc_results$V1,probs=.95), 2))
#write(electedc_constant_upper, "electedc_constant_upper.tex")


##Physical coercion, model 3-- full controls
pc_logit_pdc<-logitCoefficients(dips73, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(pc_logit_pdc))/2

electedpdc_results=as.data.frame(t(matrix(unlist(pc_logit_pdc), ncol=functional_iterations)))
electedpdc_glm<-glm(victim~elected+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73, family='binomial')
electedpdc_elected<-format(round(summary(electedpdc_glm)$coefficients[2,1], 2))
electedpdc_AIC<-format(round(summary(electedpdc_glm)$aic, 2))
#write(electedpdc_AIC, "electedpdc_AIC.tex")
#write(electedpdc_elected, "electedpdc_elected.tex")
electedpdc_lower<-format(round(quantile(electedpdc_results$V2,probs=.05), 2))
#write(electedpdc_lower, "electedpdc_lower.tex")
electedpdc_upper<-format(round(quantile(electedpdc_results$V2,probs=.95), 2))
#write(electedpdc_upper, "electedpdc_upper.tex")

electedpdc_constant<-format(round(summary(electedpdc_glm)$coefficients[1,1], 2))
#write(electedpdc_constant, "electedpdc_constant.tex")
electedpdc_constant_lower<-format(round(quantile(electedpdc_results$V1,probs=.05), 2))
#write(electedpdc_constant_lower, "electedpdc_constant_lower.tex")
electedpdc_constant_upper<-format(round(quantile(electedpdc_results$V1,probs=.95), 2))
#write(electedpdc_constant_upper, "electedpdc_constant_upper.tex")


###Logistic regression, excluding USPOO

##Any repression, model 1 -- no controls
ar_logit_noc_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_nousopo))/2

elected_anyrepression_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_logit_noc_nousopo), ncol=functional_iterations)))
elected_anyrepression_nousopo_reg<-glm(anyrepression~vote_perc, data=dips73_nousopo, family="binomial")
elected_elected_anyrepression_nousopo<-format(round(summary(elected_anyrepression_nousopo_reg)$coefficients[2,1], 2))
#write(elected_elected_anyrepression_nousopo, "elected_elected_anyrepression_nousopo.tex")
elected_anyrepression_nousopo_AIC<-format(round(summary(elected_anyrepression_nousopo_reg)$aic, 2))
#write(elected_anyrepression_nousopo_AIC, "elected_anyrepression_nousopo_AIC.tex")

elected_anyrepression_lower_nousopo<-format(round(quantile(elected_anyrepression_logit_results_nousopo$V2,probs=.05), 2))
#write(elected_anyrepression_lower_nousopo, "elected_anyrepression_lower_nousopo.tex")
elected_anyrepression_upper_nousopo<-format(round(quantile(elected_anyrepression_logit_results_nousopo$V2,probs=.95), 2))
#write(elected_anyrepression_upper_nousopo, "elected_anyrepression_upper_nousopo.tex")

elected_constant_anyrepression_nousopo<-format(round(summary(elected_anyrepression_nousopo_reg)$coefficients[1,1], 2))
#write(elected_constant_anyrepression_nousopo, "elected_constant_anyrepression_nousopo.tex")
elected_constant_anyrepression_lower_nousopo<-format(round(quantile(elected_anyrepression_logit_results_nousopo$V1,probs=.05), 2))
#write(elected_constant_anyrepression_lower_nousopo, "elected_constant_anyrepression_lower_nousopo.tex")
elected_constant_anyrepression_upper_nousopo<-format(round(quantile(elected_anyrepression_logit_results_nousopo$V1,probs=.95), 2))
#write(elected_constant_anyrepression_upper_nousopo, "elected_constant_anyrepression_upper_nousopo.tex")

##Any repression, model 2 -- party controls
ar_logit_c_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_logit_c_nousopo))/2

electedc_anyrepression_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_logit_c_nousopo), ncol=functional_iterations)))

electedc_anyrepression_nousopo_reg<-glm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedc_elected_anyrepression_nousopo<-format(round(summary(electedc_anyrepression_nousopo_reg)$coefficients[2,1], 2))
#write(electedc_elected_anyrepression_nousopo, "electedc_elected_anyrepression_nousopo.tex")
electedc_anyrepression_nousopo_AIC<-format(round(summary(electedc_anyrepression_nousopo_reg)$aic, 2))
#write(electedc_anyrepression_nousopo_AIC, "electedc_anyrepression_nousopo_AIC.tex")

electedc_anyrepression_lower_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_nousopo$V2,probs=.05), 2))
#write(electedc_anyrepression_lower_nousopo, "electedc_anyrepression_lower_nousopo.tex")
electedc_anyrepression_upper_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_nousopo$V2,probs=.95), 2))
#write(electedc_anyrepression_upper_nousopo, "electedc_anyrepression_upper_nousopo.tex")

electedc_constant_anyrepression_nousopo<-format(round(summary(electedc_anyrepression_nousopo_reg)$coefficients[1,1], 2))
#write(electedc_constant_anyrepression_nousopo, "electedc_constant_anyrepression_nousopo.tex")
electedc_constant_anyrepression_lower_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_nousopo$V1,probs=.05), 2))
#write(electedc_constant_anyrepression_lower_nousopo, "electedc_constant_anyrepression_lower_nousopo.tex")
electedc_constant_anyrepression_upper_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_nousopo$V1,probs=.95), 2))
#write(electedc_constant_anyrepression_upper_nousopo, "electedc_constant_anyrepression_upper_nousopo.tex")

##Any repression -- all covariates
ar_logit_pdc_nousopo<-logitCoefficients(dips73_nousopo, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_nousopo))/2

electedpdc_anyrepression_logit_results_nousopo=as.data.frame(t(matrix(unlist(ar_logit_pdc_nousopo), ncol=functional_iterations)))

electedpdc_anyrepression_nousopo_reg<-glm(anyrepression~vote_perc+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedpdc_elected_anyrepression_nousopo<-format(round(summary(electedpdc_anyrepression_nousopo_reg)$coefficients[2,1], 2))
#write(electedpdc_elected_anyrepression_nousopo, "electedpdc_elected_anyrepression_nousopo.tex")
electedpdc_anyrepression_nousopo_AIC<-format(round(summary(electedpdc_anyrepression_nousopo_reg)$aic, 2))
#write(electedpdc_anyrepression_nousopo_AIC, "electedpdc_anyrepression_nousopo_AIC.tex")

electedpdc_anyrepression_lower_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_nousopo$V2,probs=.05), 2))
#write(electedpdc_anyrepression_lower_nousopo, "electedpdc_anyrepression_lower_nousopo.tex")
electedpdc_anyrepression_upper_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_nousopo$V2,probs=.95), 2))
#write(electedpdc_anyrepression_upper_nousopo, "electedpdc_anyrepression_upper_nousopo.tex")

electedpdc_constant_anyrepression_nousopo<-format(round(summary(electedpdc_anyrepression_nousopo_reg)$coefficients[1,1], 2))
#write(electedpdc_constant_anyrepression_nousopo, "electedpdc_constant_anyrepression_nousopo.tex")
electedpdc_constant_anyrepression_lower_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_nousopo$V1,probs=.05), 2))
#write(electedpdc_constant_anyrepression_lower_nousopo, "electedpdc_constant_anyrepression_lower_nousopo.tex")
electedpdc_constant_anyrepression_upper_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_nousopo$V1,probs=.95), 2))
#write(electedpdc_constant_anyrepression_upper_nousopo, "electedpdc_constant_anyrepression_upper_nousopo.tex")

#Physical coercion, model 1 -- no controls
pc_logit_noc_nousopo<-logitCoefficients(dips73_nousopo, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_nousopo))/2

elected_logit_results_nousopo=as.data.frame(t(matrix(unlist(pc_logit_noc_nousopo), ncol=functional_iterations)))

elected_nousopo_reg<-glm(victim~factor(elected), data=dips73_nousopo, family="binomial")
elected_elected_nousopo<-format(round(summary(elected_nousopo_reg)$coefficients[2,1], 2))
elected_nousopo_AIC<-format(round(summary(elected_nousopo_reg)$aic, 2))
elected_nousopo_obs<-dim(dips73_nousopo)[1]
#write(elected_elected_nousopo, "elected_elected_nousopo.tex")
#write(elected_nousopo_AIC, "elected_nousopo_AIC.tex")
#write(elected_nousopo_obs, "elected_nousopo_obs.tex")

elected_lower_nousopo<-format(round(quantile(elected_logit_results_nousopo$V2,probs=.05), 2))
#write(elected_lower_nousopo, "elected_lower_nousopo.tex")
elected_upper_nousopo<-format(round(quantile(elected_logit_results_nousopo$V2,probs=.95), 2))
#write(elected_upper_nousopo, "elected_upper_nousopo.tex")

elected_constant_nousopo<-format(round(summary(elected_nousopo_reg)$coefficients[1,1], 2))
#write(elected_constant_nousopo, "elected_constant_nousopo.tex")
elected_constant_lower_nousopo<-format(round(quantile(elected_logit_results_nousopo$V1,probs=.05), 2))
#write(elected_constant_lower_nousopo, "elected_constant_lower_nousopo.tex")
elected_constant_upper_nousopo<-format(round(quantile(elected_logit_results_nousopo$V1,probs=.95), 2))
#write(elected_constant_upper_nousopo, "elected_constant_upper_nousopo.tex")

##Physical coercion, model 2 --  controls
pc_logit_c_nousopo<-logitCoefficients(dips73_nousopo, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(pc_logit_c_nousopo))/2

electedc_logit_results_nousopo=as.data.frame(t(matrix(unlist(pc_logit_c_nousopo), ncol=functional_iterations)))

electedc_nousopo_glm<-glm(victim~factor(elected)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedc_elected_nousopo<-format(round(summary(electedc_nousopo_glm)$coefficients[2,1], 2))
#write(electedc_elected_nousopo, "electedc_elected_nousopo.tex")
electedc_nousopo_AIC<-format(round(summary(electedc_nousopo_glm)$aic, 2))
#write(electedc_nousopo_AIC, "electedc_nousopo_AIC.tex")
electedc_lower_nousopo<-format(round(quantile(electedc_logit_results_nousopo$V2,probs=.05), 2))
#write(electedc_lower_nousopo, "electedc_lower_nousopo.tex")
electedc_upper_nousopo<-format(round(quantile(electedc_logit_results_nousopo$V2,probs=.95), 2))
#write(electedc_upper_nousopo, "electedc_upper_nousopo.tex")

electedc_constant_nousopo<-format(round(summary(electedc_nousopo_glm)$coefficients[1,1], 2))
#write(electedc_constant_nousopo, "electedc_constant_nousopo.tex")
electedc_constant_lower_nousopo<-format(round(quantile(electedc_logit_results_nousopo$V1,probs=.05), 2))
#write(electedc_constant_lower_nousopo, "electedc_constant_lower_nousopo.tex")
electedc_constant_upper_nousopo<-format(round(quantile(electedc_logit_results_nousopo$V1,probs=.95), 2))
#write(electedc_constant_upper_nousopo, "electedc_constant_upper_nousopo.tex")


##Physical coercion,  all covariates
pc_logit_pdc_nousopo<-logitCoefficients(dips73_nousopo, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_nousopo))/2

electedpdc_logit_results_nousopo=as.data.frame(t(matrix(unlist(pc_logit_pdc_nousopo), ncol=functional_iterations)))

electedpdc_nousopo_glm<-glm(victim~factor(elected)+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_nousopo, family="binomial")
electedpdc_elected_nousopo<-format(round(summary(electedpdc_nousopo_glm)$coefficients[2,1], 2))
electedpdc_nousopo_AIC<-format(round(summary(electedpdc_nousopo_glm)$aic, 2))
#write(electedpdc_nousopo_AIC, "electedpdc_nousopo_AIC.tex")
#write(electedpdc_elected_nousopo, "electedpdc_elected_nousopo.tex")
electedpdc_lower_nousopo<-format(round(quantile(electedpdc_logit_results_nousopo$V2,probs=.05), 2))
#write(electedpdc_lower_nousopo, "electedpdc_lower_nousopo.tex")
electedpdc_upper_nousopo<-format(round(quantile(electedpdc_logit_results_nousopo$V2,probs=.95), 2))
#write(electedpdc_upper_nousopo, "electedpdc_upper_nousopo.tex")

electedpdc_constant_nousopo<-format(round(summary(electedpdc_nousopo_glm)$coefficients[1,1], 2))
#write(electedpdc_constant_nousopo, "electedpdc_constant_nousopo.tex")
electedpdc_constant_lower_nousopo<-format(round(quantile(electedpdc_logit_results_nousopo$V1,probs=.05), 2))
#write(electedpdc_constant_lower_nousopo, "electedpdc_constant_lower_nousopo.tex")
electedpdc_constant_upper_nousopo<-format(round(quantile(electedpdc_logit_results_nousopo$V1,probs=.95), 2))
#write(electedpdc_constant_upper_nousopo, "electedpdc_constant_upper_nousopo.tex")

###Logistic regression, including only UP and PDC

##Any repression, model 1 -- no controls
ar_logit_noc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'vote_perc'), iterations)
functional_iterations=length(unlist(ar_logit_noc_leftDC_nousopo))/2

elected_anyrepression_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_logit_noc_leftDC_nousopo), ncol=functional_iterations)))
elected_anyrepression_leftDC_nousopo_reg<-glm(anyrepression~vote_perc, data=dips73_leftDC_nousopo, family="binomial")
elected_elected_anyrepression_leftDC_nousopo<-format(round(summary(elected_anyrepression_leftDC_nousopo_reg)$coefficients[2,1], 2))
#write(elected_elected_anyrepression_leftDC_nousopo, "elected_elected_anyrepression_leftDC_nousopo.tex")
elected_anyrepression_leftDC_nousopo_AIC<-format(round(summary(elected_anyrepression_leftDC_nousopo_reg)$aic, 2))
#write(elected_anyrepression_leftDC_nousopo_AIC, "elected_anyrepression_leftDC_nousopo_AIC.tex")

elected_anyrepression_lower_leftDC_nousopo<-format(round(quantile(elected_anyrepression_logit_results_leftDC_nousopo$V2,probs=.05), 2))
#write(elected_anyrepression_lower_leftDC_nousopo, "elected_anyrepression_lower_leftDC_nousopo.tex")
elected_anyrepression_upper_leftDC_nousopo<-format(round(quantile(elected_anyrepression_logit_results_leftDC_nousopo$V2,probs=.95), 2))
#write(elected_anyrepression_upper_leftDC_nousopo, "elected_anyrepression_upper_leftDC_nousopo.tex")

elected_constant_anyrepression_leftDC_nousopo<-format(round(summary(elected_anyrepression_leftDC_nousopo_reg)$coefficients[1,1], 2))
#write(elected_constant_anyrepression_leftDC_nousopo, "elected_constant_anyrepression_leftDC_nousopo.tex")
elected_constant_anyrepression_lower_leftDC_nousopo<-format(round(quantile(elected_anyrepression_logit_results_leftDC_nousopo$V1,probs=.05), 2))
#write(elected_constant_anyrepression_lower_leftDC_nousopo, "elected_constant_anyrepression_lower_leftDC_nousopo.tex")
elected_constant_anyrepression_upper_leftDC_nousopo<-format(round(quantile(elected_anyrepression_logit_results_leftDC_nousopo$V1,probs=.95), 2))
#write(elected_constant_anyrepression_upper_leftDC_nousopo, "elected_constant_anyrepression_upper_leftDC_nousopo.tex")

##Any repression, model 2 -- controls
ar_logit_c_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(ar_logit_c_leftDC_nousopo))/2

electedc_anyrepression_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_logit_c_leftDC_nousopo), ncol=functional_iterations)))

electedc_anyrepression_leftDC_nousopo_reg<-glm(anyrepression~vote_perc+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedc_elected_anyrepression_leftDC_nousopo<-format(round(summary(electedc_anyrepression_leftDC_nousopo_reg)$coefficients[2,1], 2))
#write(electedc_elected_anyrepression_leftDC_nousopo, "electedc_elected_anyrepression_leftDC_nousopo.tex")
electedc_anyrepression_leftDC_nousopo_AIC<-format(round(summary(electedc_anyrepression_leftDC_nousopo_reg)$aic, 2))
#write(electedc_anyrepression_leftDC_nousopo_AIC, "electedc_anyrepression_leftDC_nousopo_AIC.tex")

electedc_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.05), 2))
#write(electedc_anyrepression_lower_leftDC_nousopo, "electedc_anyrepression_lower_leftDC_nousopo.tex")
electedc_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.95), 2))
#write(electedc_anyrepression_upper_leftDC_nousopo, "electedc_anyrepression_upper_leftDC_nousopo.tex")

electedc_constant_anyrepression_leftDC_nousopo<-format(round(summary(electedc_anyrepression_leftDC_nousopo_reg)$coefficients[1,1], 2))
#write(electedc_constant_anyrepression_leftDC_nousopo, "electedc_constant_anyrepression_leftDC_nousopo.tex")
electedc_constant_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V1,probs=.05), 2))
#write(electedc_constant_anyrepression_lower_leftDC_nousopo, "electedc_constant_anyrepression_lower_leftDC_nousopo.tex")
electedc_constant_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V1,probs=.95), 2))
#write(electedc_constant_anyrepression_upper_leftDC_nousopo, "electedc_constant_anyrepression_upper_leftDC_nousopo.tex")


##Any repression, all covariates
ar_logit_pdc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('anyrepression', 'vote_perc', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(ar_logit_pdc_leftDC_nousopo))/2

electedpdc_anyrepression_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(ar_logit_pdc_leftDC_nousopo), ncol=functional_iterations)))

electedpdc_anyrepression_leftDC_nousopo_reg<-glm(anyrepression~vote_perc+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedpdc_elected_anyrepression_leftDC_nousopo<-format(round(summary(electedpdc_anyrepression_leftDC_nousopo_reg)$coefficients[2,1], 2))
#write(electedpdc_elected_anyrepression_leftDC_nousopo, "electedpdc_elected_anyrepression_leftDC_nousopo.tex")
electedpdc_anyrepression_leftDC_nousopo_AIC<-format(round(summary(electedpdc_anyrepression_leftDC_nousopo_reg)$aic, 2))
#write(electedpdc_anyrepression_leftDC_nousopo_AIC, "electedpdc_anyrepression_leftDC_nousopo_AIC.tex")

electedpdc_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.05), 2))
#write(electedpdc_anyrepression_lower_leftDC_nousopo, "electedpdc_anyrepression_lower_leftDC_nousopo.tex")
electedpdc_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.95), 2))
#write(electedpdc_anyrepression_upper_leftDC_nousopo, "electedpdc_anyrepression_upper_leftDC_nousopo.tex")

electedpdc_constant_anyrepression_leftDC_nousopo<-format(round(summary(electedpdc_anyrepression_leftDC_nousopo_reg)$coefficients[1,1], 2))
#write(electedpdc_constant_anyrepression_leftDC_nousopo, "electedpdc_constant_anyrepression_leftDC_nousopo.tex")
electedpdc_constant_anyrepression_lower_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V1,probs=.05), 2))
#write(electedpdc_constant_anyrepression_lower_leftDC_nousopo, "electedpdc_constant_anyrepression_lower_leftDC_nousopo.tex")
electedpdc_constant_anyrepression_upper_leftDC_nousopo<-format(round(quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V1,probs=.95), 2))
#write(electedpdc_constant_anyrepression_upper_leftDC_nousopo, "electedpdc_constant_anyrepression_upper_leftDC_nousopo.tex")

#Physical coercion, model 1 -- no controls
pc_logit_noc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('victim', 'elected'), iterations)
functional_iterations=length(unlist(pc_logit_noc_leftDC_nousopo))/2

elected_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(pc_logit_noc_leftDC_nousopo), ncol=functional_iterations)))

elected_leftDC_nousopo_reg<-glm(victim~factor(elected), data=dips73_leftDC_nousopo, family="binomial")
elected_elected_leftDC_nousopo<-format(round(summary(elected_leftDC_nousopo_reg)$coefficients[2,1], 2))
elected_leftDC_nousopo_AIC<-format(round(summary(elected_leftDC_nousopo_reg)$aic, 2))
elected_leftDC_nousopo_obs<-dim(dips73_leftDC_nousopo)[1]
#write(elected_elected_leftDC_nousopo, "elected_elected_leftDC_nousopo.tex")
#write(elected_leftDC_nousopo_AIC, "elected_leftDC_nousopo_AIC.tex")
#write(elected_leftDC_nousopo_obs, "elected_leftDC_nousopo_obs.tex")

elected_lower_leftDC_nousopo<-format(round(quantile(elected_logit_results_leftDC_nousopo$V2,probs=.05), 2))
#write(elected_lower_leftDC_nousopo, "elected_lower_leftDC_nousopo.tex")
elected_upper_leftDC_nousopo<-format(round(quantile(elected_logit_results_leftDC_nousopo$V2,probs=.95), 2))
#write(elected_upper_leftDC_nousopo, "elected_upper_leftDC_nousopo.tex")

elected_constant_leftDC_nousopo<-format(round(summary(elected_leftDC_nousopo_reg)$coefficients[1,1], 2))
#write(elected_constant_leftDC_nousopo, "elected_constant_leftDC_nousopo.tex")
elected_constant_lower_leftDC_nousopo<-format(round(quantile(elected_logit_results_leftDC_nousopo$V1,probs=.05), 2))
#write(elected_constant_lower_leftDC_nousopo, "elected_constant_lower_leftDC_nousopo.tex")
elected_constant_upper_leftDC_nousopo<-format(round(quantile(elected_logit_results_leftDC_nousopo$V1,probs=.95), 2))
#write(elected_constant_upper_leftDC_nousopo, "elected_constant_upper_leftDC_nousopo.tex")

##Physical coercion, model 2 --  controls
pc_logit_c_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected'), iterations)
functional_iterations=length(unlist(pc_logit_c_leftDC_nousopo))/2

electedc_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(pc_logit_c_leftDC_nousopo), ncol=functional_iterations)))

electedc_leftDC_nousopo_glm<-glm(victim~factor(elected)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedc_elected_leftDC_nousopo<-format(round(summary(electedc_leftDC_nousopo_glm)$coefficients[2,1], 2))
#write(electedc_elected_leftDC_nousopo, "electedc_elected_leftDC_nousopo.tex")
electedc_leftDC_nousopo_AIC<-format(round(summary(electedc_leftDC_nousopo_glm)$aic, 2))
#write(electedc_leftDC_nousopo_AIC, "electedc_leftDC_nousopo_AIC.tex")
electedc_lower_leftDC_nousopo<-format(round(quantile(electedc_logit_results_leftDC_nousopo$V2,probs=.05), 2))
#write(electedc_lower_leftDC_nousopo, "electedc_lower_leftDC_nousopo.tex")
electedc_upper_leftDC_nousopo<-format(round(quantile(electedc_logit_results_leftDC_nousopo$V2,probs=.95), 2))
#write(electedc_upper_leftDC_nousopo, "electedc_upper_leftDC_nousopo.tex")

electedc_constant_leftDC_nousopo<-format(round(summary(electedc_leftDC_nousopo_glm)$coefficients[1,1], 2))
#write(electedc_constant_leftDC_nousopo, "electedc_constant_leftDC_nousopo.tex")
electedc_constant_lower_leftDC_nousopo<-format(round(quantile(electedc_logit_results_leftDC_nousopo$V1,probs=.05), 2))
#write(electedc_constant_lower_leftDC_nousopo, "electedc_constant_lower_leftDC_nousopo.tex")
electedc_constant_upper_leftDC_nousopo<-format(round(quantile(electedc_logit_results_leftDC_nousopo$V1,probs=.95), 2))
#write(electedc_constant_upper_leftDC_nousopo, "electedc_constant_upper_leftDC_nousopo.tex")


##Physical coercion, model 3 -- all covariates
pc_logit_pdc_leftDC_nousopo<-logitCoefficients(dips73_leftDC_nousopo, c('victim', 'elected', 'surname', 'dynastic', 'incumbent_orpastelected', 'agrupacion_factor', 'list_factor'), iterations)
functional_iterations=length(unlist(pc_logit_pdc_leftDC_nousopo))/2

electedpdc_logit_results_leftDC_nousopo=as.data.frame(t(matrix(unlist(pc_logit_pdc_leftDC_nousopo), ncol=functional_iterations)))

electedpdc_leftDC_nousopo_glm<-glm(victim~factor(elected)+list+factor(agrupacion)+surname+incumbent_orpastelected+dynastic, data=dips73_leftDC_nousopo, family="binomial")
electedpdc_elected_leftDC_nousopo<-format(round(summary(electedpdc_leftDC_nousopo_glm)$coefficients[2,1], 2))
electedpdc_leftDC_nousopo_AIC<-format(round(summary(electedpdc_leftDC_nousopo_glm)$aic, 2))
#write(electedpdc_leftDC_nousopo_AIC, "electedpdc_leftDC_nousopo_AIC.tex")
#write(electedpdc_elected_leftDC_nousopo, "electedpdc_elected_leftDC_nousopo.tex")
electedpdc_lower_leftDC_nousopo<-format(round(quantile(electedpdc_logit_results_leftDC_nousopo$V2,probs=.05), 2))
#write(electedpdc_lower_leftDC_nousopo, "electedpdc_lower_leftDC_nousopo.tex")
electedpdc_upper_leftDC_nousopo<-format(round(quantile(electedpdc_logit_results_leftDC_nousopo$V2,probs=.95), 2))
#write(electedpdc_upper_leftDC_nousopo, "electedpdc_upper_leftDC_nousopo.tex")

electedpdc_constant_leftDC_nousopo<-format(round(summary(electedpdc_leftDC_nousopo_glm)$coefficients[1,1], 2))
#write(electedpdc_constant_leftDC_nousopo, "electedpdc_constant_leftDC_nousopo.tex")
electedpdc_constant_lower_leftDC_nousopo<-format(round(quantile(electedpdc_logit_results_leftDC_nousopo$V1,probs=.05), 2))
#write(electedpdc_constant_lower_leftDC_nousopo, "electedpdc_constant_lower_leftDC_nousopo.tex")
electedpdc_constant_upper_leftDC_nousopo<-format(round(quantile(electedpdc_logit_results_leftDC_nousopo$V1,probs=.95), 2))
#write(electedpdc_constant_upper_leftDC_nousopo, "electedpdc_constant_upper_leftDC_nousopo.tex")


##All candidates, controls
dips73$var3<-electedc_anyrepression_reg$coefficients[3]
dips73$var4<-electedc_anyrepression_reg$coefficients[4]
dips73$var5<-electedc_anyrepression_reg$coefficients[5]

dips73$modelparts<-dips73$var3*dips73$surname+
  dips73$var4*dips73$incumbent_orpastelected+
  dips73$var5*dips73$dynastic

anyrepression_marginaleffects<-marginalEffectsCalc(electedc_anyrepression_reg, dips73, electedc_anyrepression_reg$coefficient[2]*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_marginaleffects_lower_90<-marginalEffectsCalc(electedc_anyrepression_reg, dips73, quantile(electedc_anyrepression_results$V2,probs=.05)*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_marginaleffects_upper_90<-marginalEffectsCalc(electedc_anyrepression_reg, dips73, quantile(electedc_anyrepression_results$V2,probs=.95)*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_marginaleffects_lower_95<-marginalEffectsCalc(electedc_anyrepression_reg, dips73, quantile(electedc_anyrepression_results$V2,probs=.025)*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_marginaleffects_upper_95<-marginalEffectsCalc(electedc_anyrepression_reg, dips73, quantile(electedc_anyrepression_results$V2,probs=.975)*sd(dips73$vote_perc), dips73$modelparts)

#anyrepression, all candidates, all controls
dips73$var3<-electedpdc_anyrepression_reg$coefficients[3]
dips73$var4<-electedpdc_anyrepression_reg$coefficients[4]
dips73$var5<-electedpdc_anyrepression_reg$coefficients[5]
dips73$var6<-electedpdc_anyrepression_reg$coefficients[6]
dips73$var7<-electedpdc_anyrepression_reg$coefficients[7]
dips73$var8<-electedpdc_anyrepression_reg$coefficients[8]
dips73$var9<-electedpdc_anyrepression_reg$coefficients[9]
dips73$var10<-electedpdc_anyrepression_reg$coefficients[10]
dips73$var11<-electedpdc_anyrepression_reg$coefficients[11]
dips73$var12<-electedpdc_anyrepression_reg$coefficients[12]
dips73$var13<-electedpdc_anyrepression_reg$coefficients[13]
dips73$var14<-electedpdc_anyrepression_reg$coefficients[14]
dips73$var15<-electedpdc_anyrepression_reg$coefficients[15]
dips73$var16<-electedpdc_anyrepression_reg$coefficients[16]

summary(electedpdc_anyrepression_reg)

dips73$modelparts<-dips73$var3*I(dips73$list_factor=='UP')+
  dips73$var4*I(dips73$list_factor=="USOPO")+
  dips73$var5*I(dips73$agrupacion_factor==2)+
  dips73$var6*I(dips73$agrupacion_factor==3)+
  dips73$var7*I(dips73$agrupacion_factor==4)+
  dips73$var8*I(dips73$agrupacion_factor==5)+
  dips73$var9*I(dips73$agrupacion_factor==6)+
  dips73$var10*I(dips73$agrupacion_factor==7)+
  dips73$var11*I(dips73$agrupacion_factor==8)+
  dips73$var12*I(dips73$agrupacion_factor==9)+
  dips73$var13*I(dips73$agrupacion_factor==10)+
  dips73$var14*I(dips73$surname)+
  dips73$var15*I(dips73$dynastic)+
  dips73$var16*I(dips73$incumbent_orpastelected)

anyrepression_pdc_marginaleffects<-marginalEffectsCalc(electedpdc_anyrepression_reg, dips73, electedpdc_anyrepression_reg$coefficient[2]*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_pdc_marginaleffects_lower_90<-marginalEffectsCalc(electedpdc_anyrepression_reg, dips73, quantile(electedpdc_anyrepression_results$V2,probs=.05)*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_pdc_marginaleffects_upper_90<-marginalEffectsCalc(electedpdc_anyrepression_reg, dips73, quantile(electedpdc_anyrepression_results$V2,probs=.95)*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_pdc_marginaleffects_lower_95<-marginalEffectsCalc(electedpdc_anyrepression_reg, dips73, quantile(electedpdc_anyrepression_results$V2,probs=.025)*sd(dips73$vote_perc), dips73$modelparts)
anyrepression_pdc_marginaleffects_upper_95<-marginalEffectsCalc(electedpdc_anyrepression_reg, dips73, quantile(electedpdc_anyrepression_results$V2,probs=.975)*sd(dips73$vote_perc), dips73$modelparts)


##anyrepression, Excluding USOPO, controls
dips73_nousopo$var3<-electedc_anyrepression_nousopo_reg$coefficients[3]
dips73_nousopo$var4<-electedc_anyrepression_nousopo_reg$coefficients[4]
dips73_nousopo$var5<-electedc_anyrepression_nousopo_reg$coefficients[5]

summary(electedc_anyrepression_nousopo_reg)

dips73_nousopo$modelparts<-dips73_nousopo$var3*dips73_nousopo$surname+
  dips73_nousopo$var4*dips73_nousopo$incumbent_orpastelected+
  dips73_nousopo$var5*dips73_nousopo$dynastic

anyrepression_nousopo_marginaleffects<-marginalEffectsCalc(electedc_anyrepression_nousopo_reg, dips73_nousopo, electedc_anyrepression_nousopo_reg$coefficient[2]*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedc_anyrepression_logit_results_nousopo$V2,probs=.05)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedc_anyrepression_logit_results_nousopo$V2,probs=.95)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedc_anyrepression_logit_results_nousopo$V2,probs=.025)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedc_anyrepression_logit_results_nousopo$V2,probs=.975)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)

#anyrepression, excluding USOPO, all controls
dips73_nousopo$var3<-electedpdc_anyrepression_nousopo_reg$coefficients[3]
dips73_nousopo$var4<-electedpdc_anyrepression_nousopo_reg$coefficients[4]
dips73_nousopo$var5<-electedpdc_anyrepression_nousopo_reg$coefficients[5]
dips73_nousopo$var6<-electedpdc_anyrepression_nousopo_reg$coefficients[6]
dips73_nousopo$var7<-electedpdc_anyrepression_nousopo_reg$coefficients[7]
dips73_nousopo$var8<-electedpdc_anyrepression_nousopo_reg$coefficients[8]
dips73_nousopo$var9<-electedpdc_anyrepression_nousopo_reg$coefficients[9]
dips73_nousopo$var10<-electedpdc_anyrepression_nousopo_reg$coefficients[10]
dips73_nousopo$var11<-electedpdc_anyrepression_nousopo_reg$coefficients[11]
dips73_nousopo$var12<-electedpdc_anyrepression_nousopo_reg$coefficients[12]
dips73_nousopo$var13<-electedpdc_anyrepression_nousopo_reg$coefficients[13]
dips73_nousopo$var14<-electedpdc_anyrepression_nousopo_reg$coefficients[14]
dips73_nousopo$var15<-electedpdc_anyrepression_nousopo_reg$coefficients[15]

summary(electedpdc_anyrepression_nousopo_reg)

dips73_nousopo$modelparts<-dips73_nousopo$var3*I(dips73_nousopo$list_factor=='UP')+
  dips73_nousopo$var4*I(dips73_nousopo$agrupacion_factor==2)+
  dips73_nousopo$var5*I(dips73_nousopo$agrupacion_factor==3)+
  dips73_nousopo$var6*I(dips73_nousopo$agrupacion_factor==4)+
  dips73_nousopo$var7*I(dips73_nousopo$agrupacion_factor==5)+
  dips73_nousopo$var8*I(dips73_nousopo$agrupacion_factor==6)+
  dips73_nousopo$var9*I(dips73_nousopo$agrupacion_factor==7)+
  dips73_nousopo$var10*I(dips73_nousopo$agrupacion_factor==8)+
  dips73_nousopo$var11*I(dips73_nousopo$agrupacion_factor==9)+
  dips73_nousopo$var12*I(dips73_nousopo$agrupacion_factor==10)+
  dips73_nousopo$var13*I(dips73_nousopo$surname)+
  dips73_nousopo$var14*I(dips73_nousopo$incumbent_orpastelected)+
  dips73_nousopo$var15*I(dips73_nousopo$dynastic)

anyrepression_pdc_nousopo_marginaleffects<-marginalEffectsCalc(electedpdc_anyrepression_nousopo_reg, dips73_nousopo, electedpdc_anyrepression_nousopo_reg$coefficient[2]*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_pdc_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedpdc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedpdc_anyrepression_logit_results_nousopo$V2,probs=.05)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_pdc_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedpdc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedpdc_anyrepression_logit_results_nousopo$V2,probs=.95)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_pdc_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedpdc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedpdc_anyrepression_logit_results_nousopo$V2,probs=.025)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)
anyrepression_pdc_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedpdc_anyrepression_nousopo_reg, dips73_nousopo, quantile(electedpdc_anyrepression_logit_results_nousopo$V2,probs=.975)*sd(dips73_nousopo$vote_perc), dips73_nousopo$modelparts)


##anyrepression, UP and PDC, controls
dips73_leftDC_nousopo$var3<-electedc_anyrepression_leftDC_nousopo_reg$coefficients[3]
dips73_leftDC_nousopo$var4<-electedc_anyrepression_leftDC_nousopo_reg$coefficients[4]
dips73_leftDC_nousopo$var5<-electedc_anyrepression_leftDC_nousopo_reg$coefficients[5]

dips73_leftDC_nousopo$modelparts<-dips73_leftDC_nousopo$var3*dips73_leftDC_nousopo$surname+
  dips73_leftDC_nousopo$var4*dips73_leftDC_nousopo$incumbent_orpastelected+
  dips73_leftDC_nousopo$var5*dips73_leftDC_nousopo$dynastic

summary(electedc_anyrepression_leftDC_nousopo_reg)

anyrepression_leftDC_nousopo_marginaleffects<-marginalEffectsCalc(electedc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, electedc_anyrepression_leftDC_nousopo_reg$coefficient[2]*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_leftDC_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.05)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_leftDC_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.95)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_leftDC_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.025)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_leftDC_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.975)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)

#anyrepression, UP and PDC, all controls
dips73_leftDC_nousopo$var3<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[3]
dips73_leftDC_nousopo$var4<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[4]
dips73_leftDC_nousopo$var5<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[5]
dips73_leftDC_nousopo$var6<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[6]
dips73_leftDC_nousopo$var7<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[7]
dips73_leftDC_nousopo$var8<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[8]
dips73_leftDC_nousopo$var9<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[9]
dips73_leftDC_nousopo$var10<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[10]
dips73_leftDC_nousopo$var11<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[11]
dips73_leftDC_nousopo$var12<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[12]
dips73_leftDC_nousopo$var13<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[13]
dips73_leftDC_nousopo$var14<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[14]
dips73_leftDC_nousopo$var15<-electedpdc_anyrepression_leftDC_nousopo_reg$coefficients[15]

summary(electedpdc_anyrepression_leftDC_nousopo_reg)

dips73_leftDC_nousopo$modelparts<-dips73_leftDC_nousopo$var3*I(dips73_leftDC_nousopo$list=='UP')+
  dips73_leftDC_nousopo$var4*I(dips73_leftDC_nousopo$agrupacion_factor==2)+
  dips73_leftDC_nousopo$var5*I(dips73_leftDC_nousopo$agrupacion_factor==3)+
  dips73_leftDC_nousopo$var6*I(dips73_leftDC_nousopo$agrupacion_factor==4)+
  dips73_leftDC_nousopo$var7*I(dips73_leftDC_nousopo$agrupacion_factor==5)+
  dips73_leftDC_nousopo$var8*I(dips73_leftDC_nousopo$agrupacion_factor==6)+
  dips73_leftDC_nousopo$var9*I(dips73_leftDC_nousopo$agrupacion_factor==7)+
  dips73_leftDC_nousopo$var10*I(dips73_leftDC_nousopo$agrupacion_factor==8)+
  dips73_leftDC_nousopo$var11*I(dips73_leftDC_nousopo$agrupacion_factor==9)+
  dips73_leftDC_nousopo$var12*I(dips73_leftDC_nousopo$agrupacion_factor==10)+
  dips73_leftDC_nousopo$var13*I(dips73_leftDC_nousopo$surname)+
  dips73_leftDC_nousopo$var14*I(dips73_leftDC_nousopo$incumbent_orpastelected)+
  dips73_leftDC_nousopo$var15*I(dips73_leftDC_nousopo$dynastic)

anyrepression_pdc_leftDC_nousopo_marginaleffects<-marginalEffectsCalc(electedpdc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, electedpdc_anyrepression_leftDC_nousopo_reg$coefficient[2]*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_pdc_leftDC_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedpdc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.05)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_pdc_leftDC_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedpdc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.95)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_pdc_leftDC_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedpdc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.025)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)
anyrepression_pdc_leftDC_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedpdc_anyrepression_leftDC_nousopo_reg, dips73_leftDC_nousopo, quantile(electedpdc_anyrepression_logit_results_leftDC_nousopo$V2,probs=.975)*sd(dips73_leftDC_nousopo$vote_perc), dips73_leftDC_nousopo$modelparts)


##Physical coercion All candidates, controls
dips73$var3<-electedc_glm$coefficients[3]
dips73$var4<-electedc_glm$coefficients[4]
dips73$var5<-electedc_glm$coefficients[5]

summary(electedc_glm)
dips73$modelparts<-dips73$var3*dips73$surname+
  dips73$var4*dips73$incumbent_orpastelected+
  dips73$var5*dips73$dynastic

physicalcoercion_marginaleffects<-marginalEffectsCalc(electedc_glm, dips73, electedc_glm$coefficient[2], dips73$modelparts)
physicalcoercion_marginaleffects_lower_90<-marginalEffectsCalc(electedc_glm, dips73, quantile(electedc_results$V2,probs=.05), dips73$modelparts)
physicalcoercion_marginaleffects_upper_90<-marginalEffectsCalc(electedc_glm, dips73, quantile(electedc_results$V2,probs=.95), dips73$modelparts)
physicalcoercion_marginaleffects_lower_95<-marginalEffectsCalc(electedc_glm, dips73, quantile(electedc_results$V2,probs=.025), dips73$modelparts)
physicalcoercion_marginaleffects_upper_95<-marginalEffectsCalc(electedc_glm, dips73, quantile(electedc_results$V2,probs=.975), dips73$modelparts)

#anyrepression, all candidates, all controls
dips73$var3<-electedpdc_glm$coefficients[3]
dips73$var4<-electedpdc_glm$coefficients[4]
dips73$var5<-electedpdc_glm$coefficients[5]
dips73$var6<-electedpdc_glm$coefficients[6]
dips73$var7<-electedpdc_glm$coefficients[7]
dips73$var8<-electedpdc_glm$coefficients[8]
dips73$var9<-electedpdc_glm$coefficients[9]
dips73$var10<-electedpdc_glm$coefficients[10]
dips73$var11<-electedpdc_glm$coefficients[11]
dips73$var12<-electedpdc_glm$coefficients[12]
dips73$var13<-electedpdc_glm$coefficients[13]
dips73$var14<-electedpdc_glm$coefficients[14]
dips73$var15<-electedpdc_glm$coefficients[15]
dips73$var16<-electedpdc_glm$coefficients[16]

summary(electedpdc_glm)
dips73$modelparts<-dips73$var3*I(dips73$list_factor=='UP')+
  dips73$var4*I(dips73$list_factor=="USOPO")+
  dips73$var5*I(dips73$agrupacion_factor==2)+
  dips73$var6*I(dips73$agrupacion_factor==3)+
  dips73$var7*I(dips73$agrupacion_factor==4)+
  dips73$var8*I(dips73$agrupacion_factor==5)+
  dips73$var9*I(dips73$agrupacion_factor==6)+
  dips73$var10*I(dips73$agrupacion_factor==7)+
  dips73$var11*I(dips73$agrupacion_factor==8)+
  dips73$var12*I(dips73$agrupacion_factor==9)+
  dips73$var13*I(dips73$agrupacion_factor==10)+
  dips73$var14*I(dips73$surname)+
  dips73$var15*I(dips73$incumbent_orpastelected)+
  dips73$var16*I(dips73$dynastic)


physicalcoercion_pdc_marginaleffects<-marginalEffectsCalc(electedpdc_glm, dips73, electedpdc_glm$coefficient[2], dips73$modelparts)
physicalcoercion_pdc_marginaleffects_lower_90<-marginalEffectsCalc(electedpdc_glm, dips73, quantile(electedpdc_results$V2,probs=.05), dips73$modelparts)
physicalcoercion_pdc_marginaleffects_upper_90<-marginalEffectsCalc(electedpdc_glm, dips73, quantile(electedpdc_results$V2,probs=.95), dips73$modelparts)
physicalcoercion_pdc_marginaleffects_lower_95<-marginalEffectsCalc(electedpdc_glm, dips73, quantile(electedpdc_results$V2,probs=.025), dips73$modelparts)
physicalcoercion_pdc_marginaleffects_upper_95<-marginalEffectsCalc(electedpdc_glm, dips73, quantile(electedpdc_results$V2,probs=.975), dips73$modelparts)



##physical coercion, Excluding USOPO, controls
dips73_nousopo$var3<-electedc_nousopo_glm$coefficients[3]
dips73_nousopo$var4<-electedc_nousopo_glm$coefficients[4]
dips73_nousopo$var5<-electedc_nousopo_glm$coefficients[5]

summary(electedc_nousopo_glm)

dips73_nousopo$modelparts<-dips73_nousopo$var3*dips73_nousopo$surname+
  dips73_nousopo$var4*dips73_nousopo$incumbent_orpastelected+
  dips73_nousopo$var5*dips73_nousopo$dynastic

physicalcoercion_nousopo_marginaleffects<-marginalEffectsCalc(electedc_nousopo_glm, dips73_nousopo, electedc_nousopo_glm$coefficient[2], dips73_nousopo$modelparts)
physicalcoercion_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedc_nousopo_glm, dips73_nousopo, quantile(electedc_logit_results_nousopo$V2,probs=.05), dips73_nousopo$modelparts)
physicalcoercion_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedc_nousopo_glm, dips73_nousopo, quantile(electedc_logit_results_nousopo$V2,probs=.95), dips73_nousopo$modelparts)
physicalcoercion_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedc_nousopo_glm, dips73_nousopo, quantile(electedc_logit_results_nousopo$V2,probs=.025), dips73_nousopo$modelparts)
physicalcoercion_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedc_nousopo_glm, dips73_nousopo, quantile(electedc_logit_results_nousopo$V2,probs=.975), dips73_nousopo$modelparts)


#physical coercion, excluding USOPO, all controls
dips73_nousopo$var3<-electedpdc_nousopo_glm$coefficients[3]
dips73_nousopo$var4<-electedpdc_nousopo_glm$coefficients[4]
dips73_nousopo$var5<-electedpdc_nousopo_glm$coefficients[5]
dips73_nousopo$var6<-electedpdc_nousopo_glm$coefficients[6]
dips73_nousopo$var7<-electedpdc_nousopo_glm$coefficients[7]
dips73_nousopo$var8<-electedpdc_nousopo_glm$coefficients[8]
dips73_nousopo$var9<-electedpdc_nousopo_glm$coefficients[9]
dips73_nousopo$var10<-electedpdc_nousopo_glm$coefficients[10]
dips73_nousopo$var11<-electedpdc_nousopo_glm$coefficients[11]
dips73_nousopo$var12<-electedpdc_nousopo_glm$coefficients[12]
dips73_nousopo$var13<-electedpdc_nousopo_glm$coefficients[13]
dips73_nousopo$var14<-electedpdc_nousopo_glm$coefficients[14]
dips73_nousopo$var15<-electedpdc_nousopo_glm$coefficients[15]

summary(electedpdc_nousopo_glm)

dips73_nousopo$modelparts<-dips73_nousopo$var3*I(dips73_nousopo$list_factor=='UP')+
  dips73_nousopo$var4*I(dips73_nousopo$agrupacion_factor==2)+
  dips73_nousopo$var5*I(dips73_nousopo$agrupacion_factor==3)+
  dips73_nousopo$var6*I(dips73_nousopo$agrupacion_factor==4)+
  dips73_nousopo$var7*I(dips73_nousopo$agrupacion_factor==5)+
  dips73_nousopo$var8*I(dips73_nousopo$agrupacion_factor==6)+
  dips73_nousopo$var9*I(dips73_nousopo$agrupacion_factor==7)+
  dips73_nousopo$var10*I(dips73_nousopo$agrupacion_factor==8)+
  dips73_nousopo$var11*I(dips73_nousopo$agrupacion_factor==9)+
  dips73_nousopo$var12*I(dips73_nousopo$agrupacion_factor==10)+
  dips73_nousopo$var13*I(dips73_nousopo$surname)+
  dips73_nousopo$var14*I(dips73_nousopo$incumbent_orpastelected)+
  dips73_nousopo$var15*I(dips73_nousopo$dynastic)

physicalcoercion_pdc_nousopo_marginaleffects<-marginalEffectsCalc(electedpdc_nousopo_glm, dips73_nousopo, electedpdc_nousopo_glm$coefficient[2], dips73_nousopo$modelparts)
physicalcoercion_pdc_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedpdc_nousopo_glm, dips73_nousopo, quantile(electedpdc_logit_results_nousopo$V2,probs=.05), dips73_nousopo$modelparts)
physicalcoercion_pdc_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedpdc_nousopo_glm, dips73_nousopo, quantile(electedpdc_logit_results_nousopo$V2,probs=.95), dips73_nousopo$modelparts)
physicalcoercion_pdc_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedpdc_nousopo_glm, dips73_nousopo, quantile(electedpdc_logit_results_nousopo$V2,probs=.025), dips73_nousopo$modelparts)
physicalcoercion_pdc_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedpdc_nousopo_glm, dips73_nousopo, quantile(electedpdc_logit_results_nousopo$V2,probs=.975), dips73_nousopo$modelparts)


##physical coercion, UP and PDC, controls
dips73_leftDC_nousopo$var3<-electedc_leftDC_nousopo_glm$coefficients[3]
dips73_leftDC_nousopo$var4<-electedc_leftDC_nousopo_glm$coefficients[4]
dips73_leftDC_nousopo$var5<-electedc_leftDC_nousopo_glm$coefficients[5]

summary(electedc_leftDC_nousopo_glm)

dips73_leftDC_nousopo$modelparts<-dips73_leftDC_nousopo$var3*dips73_leftDC_nousopo$surname+
  dips73_leftDC_nousopo$var4*dips73_leftDC_nousopo$incumbent_orpastelected+
  dips73_leftDC_nousopo$var5*dips73_leftDC_nousopo$dynastic

physicalcoercion_leftDC_nousopo_marginaleffects<-marginalEffectsCalc(electedc_leftDC_nousopo_glm, dips73_leftDC_nousopo, electedc_leftDC_nousopo_glm$coefficient[2], dips73_leftDC_nousopo$modelparts)
physicalcoercion_leftDC_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedc_logit_results_leftDC_nousopo$V2,probs=.05), dips73_leftDC_nousopo$modelparts)
physicalcoercion_leftDC_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedc_logit_results_leftDC_nousopo$V2,probs=.95), dips73_leftDC_nousopo$modelparts)
physicalcoercion_leftDC_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedc_logit_results_leftDC_nousopo$V2,probs=.025), dips73_leftDC_nousopo$modelparts)
physicalcoercion_leftDC_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedc_logit_results_leftDC_nousopo$V2,probs=.975), dips73_leftDC_nousopo$modelparts)

#anyrepression, UP and PDC, all controls
dips73_leftDC_nousopo$var3<-electedpdc_leftDC_nousopo_glm$coefficients[3]
dips73_leftDC_nousopo$var4<-electedpdc_leftDC_nousopo_glm$coefficients[4]
dips73_leftDC_nousopo$var5<-electedpdc_leftDC_nousopo_glm$coefficients[5]
dips73_leftDC_nousopo$var6<-electedpdc_leftDC_nousopo_glm$coefficients[6]
dips73_leftDC_nousopo$var7<-electedpdc_leftDC_nousopo_glm$coefficients[7]
dips73_leftDC_nousopo$var8<-electedpdc_leftDC_nousopo_glm$coefficients[8]
dips73_leftDC_nousopo$var9<-electedpdc_leftDC_nousopo_glm$coefficients[9]
dips73_leftDC_nousopo$var10<-electedpdc_leftDC_nousopo_glm$coefficients[10]
dips73_leftDC_nousopo$var11<-electedpdc_leftDC_nousopo_glm$coefficients[11]
dips73_leftDC_nousopo$var12<-electedpdc_leftDC_nousopo_glm$coefficients[12]
dips73_leftDC_nousopo$var13<-electedpdc_leftDC_nousopo_glm$coefficients[13]
dips73_leftDC_nousopo$var14<-electedpdc_leftDC_nousopo_glm$coefficients[14]
dips73_leftDC_nousopo$var15<-electedpdc_leftDC_nousopo_glm$coefficients[15]

summary(electedpdc_leftDC_nousopo_glm)

dips73_leftDC_nousopo$modelparts<-dips73_leftDC_nousopo$var3*I(dips73_leftDC_nousopo$list_factor=='UP')+
  dips73_leftDC_nousopo$var4*I(dips73_leftDC_nousopo$agrupacion_factor==2)+
  dips73_leftDC_nousopo$var5*I(dips73_leftDC_nousopo$agrupacion_factor==3)+
  dips73_leftDC_nousopo$var6*I(dips73_leftDC_nousopo$agrupacion_factor==4)+
  dips73_leftDC_nousopo$var7*I(dips73_leftDC_nousopo$agrupacion_factor==5)+
  dips73_leftDC_nousopo$var8*I(dips73_leftDC_nousopo$agrupacion_factor==6)+
  dips73_leftDC_nousopo$var9*I(dips73_leftDC_nousopo$agrupacion_factor==7)+
  dips73_leftDC_nousopo$var10*I(dips73_leftDC_nousopo$agrupacion_factor==8)+
  dips73_leftDC_nousopo$var11*I(dips73_leftDC_nousopo$agrupacion_factor==9)+
  dips73_leftDC_nousopo$var12*I(dips73_leftDC_nousopo$agrupacion_factor==10)+
  dips73_leftDC_nousopo$var13*I(dips73_leftDC_nousopo$surname)+
  dips73_leftDC_nousopo$var14*I(dips73_leftDC_nousopo$incumbent_orpastelected)+
  dips73_leftDC_nousopo$var15*I(dips73_leftDC_nousopo$dynastic)

physicalcoercion_pdc_leftDC_nousopo_marginaleffects<-marginalEffectsCalc(electedpdc_leftDC_nousopo_glm, dips73_leftDC_nousopo, electedpdc_leftDC_nousopo_glm$coefficient[2], dips73_leftDC_nousopo$modelparts)
physicalcoercion_pdc_leftDC_nousopo_marginaleffects_lower_90<-marginalEffectsCalc(electedpdc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedpdc_logit_results_leftDC_nousopo$V2,probs=.05), dips73_leftDC_nousopo$modelparts)
physicalcoercion_pdc_leftDC_nousopo_marginaleffects_upper_90<-marginalEffectsCalc(electedpdc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedpdc_logit_results_leftDC_nousopo$V2,probs=.95), dips73_leftDC_nousopo$modelparts)
physicalcoercion_pdc_leftDC_nousopo_marginaleffects_lower_95<-marginalEffectsCalc(electedpdc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedpdc_logit_results_leftDC_nousopo$V2,probs=.025), dips73_leftDC_nousopo$modelparts)
physicalcoercion_pdc_leftDC_nousopo_marginaleffects_upper_95<-marginalEffectsCalc(electedpdc_leftDC_nousopo_glm, dips73_leftDC_nousopo, quantile(electedpdc_logit_results_leftDC_nousopo$V2,probs=.975), dips73_leftDC_nousopo$modelparts)



anyrepression_marginaleffects_df<-data.frame(rbind(cbind(anyrepression_marginaleffects, anyrepression_marginaleffects_lower_90, anyrepression_marginaleffects_upper_90, '90%', '6. anyrepression_all_c'),
                                                   cbind(anyrepression_marginaleffects, anyrepression_marginaleffects_lower_95, anyrepression_marginaleffects_upper_95, '95%', '6. anyrepression_all_c'),
                                                   cbind(anyrepression_pdc_marginaleffects, anyrepression_pdc_marginaleffects_lower_90, anyrepression_pdc_marginaleffects_upper_90, '90%', '5. anyrepression_all_pdc'),
                                                   cbind(anyrepression_pdc_marginaleffects, anyrepression_pdc_marginaleffects_lower_95, anyrepression_pdc_marginaleffects_upper_95, '95%', '5. anyrepression_all_pdc'),
                                                   cbind(anyrepression_nousopo_marginaleffects, anyrepression_nousopo_marginaleffects_lower_90, anyrepression_nousopo_marginaleffects_upper_90, '90%', '4. anyrepression_nousopo_c'),
                                                   cbind(anyrepression_nousopo_marginaleffects, anyrepression_nousopo_marginaleffects_lower_95, anyrepression_nousopo_marginaleffects_upper_95, '95%', '4. anyrepression_nousopo_c'),
                                                   cbind(anyrepression_pdc_nousopo_marginaleffects, anyrepression_pdc_nousopo_marginaleffects_lower_90, anyrepression_pdc_nousopo_marginaleffects_upper_90, '90%', '3. anyrepression_nousopo_pdc'),
                                                   cbind(anyrepression_pdc_nousopo_marginaleffects, anyrepression_pdc_nousopo_marginaleffects_lower_95, anyrepression_pdc_nousopo_marginaleffects_upper_95, '95%', '3. anyrepression_nousopo_pdc'),
                                                   cbind(anyrepression_leftDC_nousopo_marginaleffects, anyrepression_leftDC_nousopo_marginaleffects_lower_90, anyrepression_leftDC_nousopo_marginaleffects_upper_90, '90%', '2. anyrepression_leftDC_nousopo_c'),
                                                   cbind(anyrepression_leftDC_nousopo_marginaleffects, anyrepression_leftDC_nousopo_marginaleffects_lower_95, anyrepression_leftDC_nousopo_marginaleffects_upper_95, '95%', '2. anyrepression_leftDC_nousopo_c'),
                                                   cbind(anyrepression_pdc_leftDC_nousopo_marginaleffects, anyrepression_pdc_leftDC_nousopo_marginaleffects_lower_90, anyrepression_pdc_leftDC_nousopo_marginaleffects_upper_90, '90%', '1. anyrepression_leftDC_nousopo_pdc'),
                                                   cbind(anyrepression_pdc_leftDC_nousopo_marginaleffects, anyrepression_pdc_leftDC_nousopo_marginaleffects_lower_95, anyrepression_pdc_leftDC_nousopo_marginaleffects_upper_95, '95%', '1. anyrepression_leftDC_nousopo_pdc')))

colnames(anyrepression_marginaleffects_df)<-c('effect', 'lower', 'upper', 'CI', 'type')

physicalcoercion_marginaleffects_df<-data.frame(rbind(cbind(physicalcoercion_marginaleffects, physicalcoercion_marginaleffects_lower_90, physicalcoercion_marginaleffects_upper_90, '90%', '6. physicalcoercion_all_c'),
                                                      cbind(physicalcoercion_marginaleffects, physicalcoercion_marginaleffects_lower_95, physicalcoercion_marginaleffects_upper_95, '95%', '6. physicalcoercion_all_c'),
                                                      cbind(physicalcoercion_pdc_marginaleffects, physicalcoercion_pdc_marginaleffects_lower_90, physicalcoercion_pdc_marginaleffects_upper_90, '90%', '5. physicalcoercion_all_pdc'),
                                                      cbind(physicalcoercion_pdc_marginaleffects, physicalcoercion_pdc_marginaleffects_lower_95, physicalcoercion_pdc_marginaleffects_upper_95, '95%', '5. physicalcoercion_all_pdc'),
                                                      cbind(physicalcoercion_nousopo_marginaleffects, physicalcoercion_nousopo_marginaleffects_lower_90, physicalcoercion_nousopo_marginaleffects_upper_90, '90%', '4. physicalcoercion_nousopo_c'),
                                                      cbind(physicalcoercion_nousopo_marginaleffects, physicalcoercion_nousopo_marginaleffects_lower_95, physicalcoercion_nousopo_marginaleffects_upper_95, '95%', '4. physicalcoercion_nousopo_c'),
                                                      cbind(physicalcoercion_pdc_nousopo_marginaleffects, physicalcoercion_pdc_nousopo_marginaleffects_lower_90, physicalcoercion_pdc_nousopo_marginaleffects_upper_90, '90%', '3. physicalcoercion_nousopo_pdc'),
                                                      cbind(physicalcoercion_pdc_nousopo_marginaleffects, physicalcoercion_pdc_nousopo_marginaleffects_lower_95, physicalcoercion_pdc_nousopo_marginaleffects_upper_95, '95%', '3. physicalcoercion_nousopo_pdc'),
                                                      cbind(physicalcoercion_leftDC_nousopo_marginaleffects, physicalcoercion_leftDC_nousopo_marginaleffects_lower_90, physicalcoercion_leftDC_nousopo_marginaleffects_upper_90, '90%', '2. physicalcoercion_leftDC_nousopo_c'),
                                                      cbind(physicalcoercion_leftDC_nousopo_marginaleffects, physicalcoercion_leftDC_nousopo_marginaleffects_lower_95, physicalcoercion_leftDC_nousopo_marginaleffects_upper_95, '95%', '2. physicalcoercion_leftDC_nousopo_c'),
                                                      cbind(physicalcoercion_pdc_leftDC_nousopo_marginaleffects, physicalcoercion_pdc_leftDC_nousopo_marginaleffects_lower_90, physicalcoercion_pdc_leftDC_nousopo_marginaleffects_upper_90, '90%', '1. physicalcoercion_leftDC_nousopo_pdc'),
                                                      cbind(physicalcoercion_pdc_leftDC_nousopo_marginaleffects, physicalcoercion_pdc_leftDC_nousopo_marginaleffects_lower_95, physicalcoercion_pdc_leftDC_nousopo_marginaleffects_upper_95, '95%', '1. physicalcoercion_leftDC_nousopo_pdc')))
colnames(physicalcoercion_marginaleffects_df)<-c('effect', 'lower', 'upper', 'CI', 'type')


anyrepression_marginaleffects<-ggplot(anyrepression_marginaleffects_df, aes(x=type, y=as.numeric(as.character(effect)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(anyrepression_marginaleffects_df$lower)), 
                             ymax=as.numeric(as.character(anyrepression_marginaleffects_df$upper)))+
  ylim(-.05, .3)+scale_linetype_manual(values=c('solid', 'dotted'),
                                       guide = guide_legend(override.aes = list(
                                         linetype = c("solid", "dotted"),
                                         shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  ylab("") +
  xlab("")+scale_x_discrete(labels=c('UP and PDC (full controls)', 'UP and PDC', 'Excluding USOPO (full controls)',
                                     'Excluding USOPO', 'All candidates (full controls)', 'All candidates'))
#ggsave('anyrepression_marginaleffects_plot.pdf', plot=anyrepression_marginaleffects, height=5, width=5.25)



physicalcoercion_marginaleffects<-ggplot(physicalcoercion_marginaleffects_df, aes(x=type, y=as.numeric(as.character(effect)), group=CI, linetype=CI))+
  geom_point()+
  theme_bw()+geom_pointrange(size=.5, ymin=as.numeric(as.character(physicalcoercion_marginaleffects_df$lower)), 
                             ymax=as.numeric(as.character(physicalcoercion_marginaleffects_df$upper)))+
  ylim(-.2, .1)+scale_linetype_manual(values=c('solid', 'dotted'),
                                      guide = guide_legend(override.aes = list(
                                        linetype = c("solid", "dotted"),
                                        shape=c(NA, NA)) ))+
  theme(legend.position="bottom",  axis.title=element_text(size=16),
        text=element_text(family="Times New Roman", size=16),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  coord_flip() +
  geom_hline(aes(yintercept=0), lty=4) +
  ylab("") +
  xlab("")+scale_x_discrete(labels=c('UP and PDC (full controls)', 'UP and PDC', 'Excluding USOPO (full controls)',
                                     'Excluding USOPO', 'All candidates (full controls)', 'All candidates'))
#ggsave('physicalcoercion_marginaleffects_plot.pdf', plot=physicalcoercion_marginaleffects, height=5, width=5.25)


############################################################
##########Figure 3 -- repression by quotient share##########
############################################################

anyrepression_plot<-whiskerPlotLevels(dips73, dips73$anyrepression, " ", "Overall repression mean", "All candidates")
anyrepression_leftDC_plot<-whiskerPlotLevels(dips73_leftDC_nousopo, dips73_leftDC_nousopo$anyrepression, " ", " ", "UP and PDC Candidates")

physicalcoercion_plot<-whiskerPlotLevels(dips73, dips73$victim, "Group", "Physical coercion mean", " ")
physicalcoercion_leftDC_plot<-whiskerPlotLevels(dips73_leftDC_nousopo, dips73_leftDC_nousopo$victim, "Group", " ", " ")

pdf("victim_anyrepressionplots.pdf", width=10, height=8, family="Times New Roman")
multiplot(anyrepression_plot, physicalcoercion_plot, anyrepression_leftDC_plot, physicalcoercion_leftDC_plot, cols=2)
dev.off()


##Table 1 -- LLR, any repression
rdd_all_anyrepression_nousopo<-RDestimate(anyrepression~quotient_diff, data=dips73, cluster=dips73$Distrito)
rdd_all_party_anyrepression_nousopo<-RDestimate(anyrepression~quotient_diff|Partido, data=dips73, cluster=dips73_nousopo$Distrito)

#write(format(round(rdd_all_anyrepression_nousopo$est[1], 2)), "late_anyrepression_nousopo.tex")
#write(format(round(rdd_all_anyrepression_nousopo$est[2], 2)), "half_bw_anyrepression_nousopo.tex")
#write(format(round(rdd_all_anyrepression_nousopo$est[3], 2)), "double_bw_anyrepression_nousopo.tex")

#write(format(round(rdd_all_party_anyrepression_nousopo$est[1], 2)), "latep_anyrepression_nousopo.tex")
#write(format(round(rdd_all_party_anyrepression_nousopo$est[2], 2)), "halfp_anyrepression_nousopo.tex")
#write(format(round(rdd_all_party_anyrepression_nousopo$est[3], 2)), "doublep_anyrepression_nousopo.tex")

#write(rdd_all_anyrepression_nousopo$obs[1], "obs_late_anyrepression_nousopo.tex")
#write(rdd_all_anyrepression_nousopo$obs[2], "obs_half_late_anyrepression_nousopo.tex")
#write(rdd_all_anyrepression_nousopo$obs[3], "obs_double_late_anyrepression_nousopo.tex")

#write(format(round(rdd_all_anyrepression_nousopo$bw[1],2)), "bw_late_anyrepression_nousopo.tex")
#write(format(round(rdd_all_anyrepression_nousopo$bw[2],2)), "bw_half_late_anyrepression_nousopo.tex")
#write(format(round(rdd_all_anyrepression_nousopo$bw[3],2)), "bw_double_late_anyrepression_nousopo.tex")

LLR_anyrepression_results<-bootstrapLLR(LLR_anyrepression_data, iterations)

late_lower_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLR, probs=.05), 2))
late_upper_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLR, probs=.95), 2))
#write(late_lower_anyrepression_nousopo, "late_lower_anyrepression_nousopo.tex")
#write(late_upper_anyrepression_nousopo, "late_upper_anyrepression_nousopo.tex")

late_half_lower_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRhalf, probs=.05), 2))
late_half_upper_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRhalf, probs=.95), 2))
#write(late_half_lower_anyrepression_nousopo, "late_half_lower_anyrepression_nousopo.tex")
#write(late_half_upper_anyrepression_nousopo, "late_half_upper_anyrepression_nousopo.tex")

late_2_lower_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLR2, probs=.05), 2))
late_2_upper_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLR2, probs=.95), 2))
#write(late_2_lower_anyrepression_nousopo, "late_2_lower_anyrepression_nousopo.tex")
#write(late_2_upper_anyrepression_nousopo, "late_2_upper_anyrepression_nousopo.tex")

late_party_lower_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRparty, probs=.05), 2))
late_party_upper_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRparty, probs=.95), 2))
#write(late_party_lower_anyrepression_nousopo, "late_party_lower_anyrepression_nousopo.tex")
#write(late_party_upper_anyrepression_nousopo, "late_party_upper_anyrepression_nousopo.tex")

late_party_half_lower_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRpartyhalf, probs=.95), 2))
#write(late_party_half_lower_anyrepression_nousopo, "late_party_half_lower_anyrepression_nousopo.tex")
#write(late_party_half_upper_anyrepression_nousopo, "late_party_half_upper_anyrepression_nousopo.tex")

late_party_2_lower_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRparty2, probs=.05), 2))
late_party_2_upper_anyrepression_nousopo<-format(round(quantile(LLR_anyrepression_results$LLRparty2, probs=.95), 2))
#write(late_party_2_lower_anyrepression_nousopo, "late_party_2_lower_anyrepression_nousopo.tex")
#write(late_party_2_upper_anyrepression_nousopo, "late_party_2_upper_anyrepression_nousopo.tex")



late_lower_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLR, probs=.025), 2))
late_upper_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLR, probs=.975), 2))
#write(late_lower_anyrepression_nousopo_95, "late_lower_anyrepression_nousopo_95.tex")
#write(late_upper_anyrepression_nousopo_95, "late_upper_anyrepression_nousopo_95.tex")

late_half_lower_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRhalf, probs=.025), 2))
late_half_upper_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRhalf, probs=.975), 2))
#write(late_half_lower_anyrepression_nousopo_95, "late_half_lower_anyrepression_nousopo_95.tex")
#write(late_half_upper_anyrepression_nousopo_95, "late_half_upper_anyrepression_nousopo_95.tex")

late_2_lower_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLR2, probs=.025), 2))
late_2_upper_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLR2, probs=.975), 2))
#write(late_2_lower_anyrepression_nousopo_95, "late_2_lower_anyrepression_nousopo_95.tex")
#write(late_2_upper_anyrepression_nousopo_95, "late_2_upper_anyrepression_nousopo_95.tex")

late_party_lower_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRparty, probs=.025), 2))
late_party_upper_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRparty, probs=.975), 2))
#write(late_party_lower_anyrepression_nousopo_95, "late_party_lower_anyrepression_nousopo_95.tex")
#write(late_party_upper_anyrepression_nousopo_95, "late_party_upper_anyrepression_nousopo_95.tex")

late_party_half_lower_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRpartyhalf, probs=.975), 2))
#write(late_party_half_lower_anyrepression_nousopo_95, "late_party_half_lower_anyrepression_nousopo_95.tex")
#write(late_party_half_upper_anyrepression_nousopo_95, "late_party_half_upper_anyrepression_nousopo_95.tex")

late_party_2_lower_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRparty2, probs=.025), 2))
late_party_2_upper_anyrepression_nousopo_95<-format(round(quantile(LLR_anyrepression_results$LLRparty2, probs=.975), 2))
#write(late_party_2_lower_anyrepression_nousopo_95, "late_party_2_lower_anyrepression_nousopo_95.tex")
#write(late_party_2_upper_anyrepression_nousopo_95, "late_party_2_upper_anyrepression_nousopo_95.tex")



##########################################
####Table 1 -- LLR, physical coercion#####
##########################################

rdd_all_physicalcoercion_nousopo<-RDestimate(victim~quotient_diff, data=dips73, cluster=dips73$Distrito)
rdd_all_party_physicalcoercion_nousopo<-RDestimate(victim~quotient_diff|Partido, data=dips73, cluster=dips73$Distrito)

#write(format(round(rdd_all_physicalcoercion_nousopo$est[1], 2)), "late_physicalcoercion_nousopo.tex")
#write(format(round(rdd_all_physicalcoercion_nousopo$est[2], 2)), "half_bw_physicalcoercion_nousopo.tex")
#write(format(round(rdd_all_physicalcoercion_nousopo$est[3], 2)), "double_bw_physicalcoercion_nousopo.tex")

#write(format(round(rdd_all_party_physicalcoercion_nousopo$est[1], 2)), "latep_physicalcoercion_nousopo.tex")
#write(format(round(rdd_all_party_physicalcoercion_nousopo$est[2], 2)), "halfp_physicalcoercion_nousopo.tex")
#write(format(round(rdd_all_party_physicalcoercion_nousopo$est[3], 2)), "doublep_physicalcoercion_nousopo.tex")

#write(rdd_all_physicalcoercion_nousopo$obs[1], "obs_late_physicalcoercion_nousopo.tex")
#write(rdd_all_physicalcoercion_nousopo$obs[2], "obs_half_late_physicalcoercion_nousopo.tex")
#write(rdd_all_physicalcoercion_nousopo$obs[3], "obs_double_late_physicalcoercion_nousopo.tex")

#write(format(round(rdd_all_physicalcoercion_nousopo$bw[1],2)), "bw_late_physicalcoercion_nousopo.tex")
#write(format(round(rdd_all_physicalcoercion_nousopo$bw[2],2)), "bw_half_late_physicalcoercion_nousopo.tex")
#write(format(round(rdd_all_physicalcoercion_nousopo$bw[3],2)), "bw_double_late_physicalcoercion_nousopo.tex")

LLR_physicalcoercion_results<-bootstrapLLR(LLR_physicalcoercion_data, iterations)

late_lower_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLR, probs=.05), 2))
late_upper_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLR, probs=.95), 2))
#write(late_lower_physicalcoercion_nousopo, "late_lower_physicalcoercion_nousopo.tex")
#write(late_upper_physicalcoercion_nousopo, "late_upper_physicalcoercion_nousopo.tex")

late_half_lower_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRhalf, probs=.05), 2))
late_half_upper_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRhalf, probs=.95), 2))
#write(late_half_lower_physicalcoercion_nousopo, "late_half_lower_physicalcoercion_nousopo.tex")
#write(late_half_upper_physicalcoercion_nousopo, "late_half_upper_physicalcoercion_nousopo.tex")

late_2_lower_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLR2, probs=.05), 2))
late_2_upper_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLR2, probs=.95), 2))
#write(late_2_lower_physicalcoercion_nousopo, "late_2_lower_physicalcoercion_nousopo.tex")
#write(late_2_upper_physicalcoercion_nousopo, "late_2_upper_physicalcoercion_nousopo.tex")

late_party_lower_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRparty, probs=.05), 2))
late_party_upper_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRparty, probs=.95), 2))
#write(late_party_lower_physicalcoercion_nousopo, "late_party_lower_physicalcoercion_nousopo.tex")
#write(late_party_upper_physicalcoercion_nousopo, "late_party_upper_physicalcoercion_nousopo.tex")

late_party_half_lower_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRpartyhalf, probs=.05), 2))
late_party_half_upper_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRpartyhalf, probs=.95), 2))
#write(late_party_half_lower_physicalcoercion_nousopo, "late_party_half_lower_physicalcoercion_nousopo.tex")
#write(late_party_half_upper_physicalcoercion_nousopo, "late_party_half_upper_physicalcoercion_nousopo.tex")

late_party_2_lower_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRparty2, probs=.05), 2))
late_party_2_upper_physicalcoercion_nousopo<-format(round(quantile(LLR_physicalcoercion_results$LLRparty2, probs=.95), 2))
#write(late_party_2_lower_physicalcoercion_nousopo, "late_party_2_lower_physicalcoercion_nousopo.tex")
#write(late_party_2_upper_physicalcoercion_nousopo, "late_party_2_upper_physicalcoercion_nousopo.tex")



late_lower_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLR, probs=.025), 2))
late_upper_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLR, probs=.975), 2))
#write(late_lower_physicalcoercion_nousopo_95, "late_lower_physicalcoercion_nousopo_95.tex")
#write(late_upper_physicalcoercion_nousopo_95, "late_upper_physicalcoercion_nousopo_95.tex")

late_half_lower_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRhalf, probs=.025), 2))
late_half_upper_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRhalf, probs=.975), 2))
#write(late_half_lower_physicalcoercion_nousopo_95, "late_half_lower_physicalcoercion_nousopo_95.tex")
#write(late_half_upper_physicalcoercion_nousopo_95, "late_half_upper_physicalcoercion_nousopo_95.tex")

late_2_lower_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLR2, probs=.025), 2))
late_2_upper_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLR2, probs=.975), 2))
#write(late_2_lower_physicalcoercion_nousopo_95, "late_2_lower_physicalcoercion_nousopo_95.tex")
#write(late_2_upper_physicalcoercion_nousopo_95, "late_2_upper_physicalcoercion_nousopo_95.tex")

late_party_lower_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRparty, probs=.025), 2))
late_party_upper_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRparty, probs=.975), 2))
#write(late_party_lower_physicalcoercion_nousopo_95, "late_party_lower_physicalcoercion_nousopo_95.tex")
#write(late_party_upper_physicalcoercion_nousopo_95, "late_party_upper_physicalcoercion_nousopo_95.tex")

late_party_half_lower_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRpartyhalf, probs=.025), 2))
late_party_half_upper_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRpartyhalf, probs=.975), 2))
#write(late_party_half_lower_physicalcoercion_nousopo_95, "late_party_half_lower_physicalcoercion_nousopo_95.tex")
#write(late_party_half_upper_physicalcoercion_nousopo_95, "late_party_half_upper_physicalcoercion_nousopo_95.tex")

late_party_2_lower_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRparty2, probs=.025), 2))
late_party_2_upper_physicalcoercion_nousopo_95<-format(round(quantile(LLR_physicalcoercion_results$LLRparty2, probs=.975), 2))
#write(late_party_2_lower_physicalcoercion_nousopo_95, "late_party_2_lower_physicalcoercion_nousopo_95.tex")
#write(late_party_2_upper_physicalcoercion_nousopo_95, "late_party_2_upper_physicalcoercion_nousopo_95.tex")



#################################################
############Figure 4 -- RD plot##################
#################################################

#pdf('rdplot.pdf',family="Times New Roman", width=10, height=6)
plot(rdd_all_physicalcoercion_nousopo, range=c(-4, 4))
#dev.off()


##################################################
#########Figure 5 -- press coverage###############
###################################################

ttest_presscoverage<-ttest_electedvunelected(dips73$CD_presscoverage, dips73)

#write(format(round(as.numeric(as.character(ttest_presscoverage$elected[1])), 2)), "mean_presscoverage_elected.tex")
#write(format(round(as.numeric(as.character(ttest_presscoverage$unelected[1])), 2)), "mean_presscoverage_unelected.tex")
#write(format(round(as.numeric(as.character(ttest_presscoverage$difference[1])), 2)), "mean_presscoverage_difference.tex")

ttest_presscoverage_nousopo<-ttest_electedvunelected(dips73_nousopo$CD_presscoverage, dips73_nousopo)

#write(format(round(as.numeric(as.character(ttest_presscoverage_nousopo$elected[1])), 2)), "mean_presscoverage_elected_nousopo.tex")
#write(format(round(as.numeric(as.character(ttest_presscoverage_nousopo$unelected[1])), 2)), "mean_presscoverage_unelected_nousopo.tex")
#write(format(round(as.numeric(as.character(ttest_presscoverage_nousopo$difference[1])), 2)), "mean_presscoverage_difference_nousopo.tex")

ttest_presscoverage_left<-ttest_electedvunelected(dips73_leftDC_nousopo$CD_presscoverage, dips73_leftDC_nousopo)

#write(format(round(as.numeric(as.character(ttest_presscoverage_left$elected[1])), 2)), "mean_presscoverage_elected_left_nousopo.tex")
#write(format(round(as.numeric(as.character(ttest_presscoverage_left$unelected[1])), 2)), "mean_presscoverage_unelected_left_nousopo.tex")
#write(format(round(as.numeric(as.character(ttest_presscoverage_left$difference[1])), 2)), "mean_presscoverage_difference_left_nousopo.tex")

press_plot<-whiskerPlotLevels(dips73, dips73$CD_presscoverage, "Group", "Press coverage mean", "")

#pdf("press_plot_close.pdf", width=6, height=5, family="Times New Roman")
press_plot
#dev.off()

rdd_all_presscoverage<-RDestimate(CD_presscoverage~quotient_diff, data=dips73, cluster=dips73$Distrito)

#pdf('rdd_all_presscoverage.pdf',family="Times New Roman", width=8, height=6)
plot(rdd_all_presscoverage, range=c(-4, 4))
#dev.off()


#####Target lists
targeted_ttest<-t.test(dips73$search[dips73$elected==1], dips73$search[dips73$elected==0])
#write(format(round(targeted_ttest$estimate[1]*100, 1)), 'targeted_elected.tex')
#write(format(round(targeted_ttest$estimate[2]*100, 1)), 'targeted_unelected.tex')
#write(format(round(targeted_ttest$p.value, 2)), 'targeted_pval.tex')

anyrepression_targeted<-t.test(dips73$anyrepression[dips73$search==1&dips73$elected==1], dips73$anyrepression[dips73$search==1&dips73$elected==0])
physicalcoercion_targeted<-t.test(dips73$victim[dips73$search==1&dips73$elected==1], dips73$victim[dips73$search==1&dips73$elected==0])

anyrepression_targeted_UPOnly<-t.test(dips73$anyrepression[dips73$search==1&dips73$elected==1&dips73$list=='UP'], dips73$anyrepression[dips73$search==1&dips73$elected==0&dips73$list=='UP'])
physicalcoercion_targeted_UPOnly<-t.test(dips73$victim[dips73$search==1&dips73$elected==1&dips73$list=='UP'], dips73$victim[dips73$search==1&dips73$elected==0&dips73$list=='UP'])

#write(format(round(anyrepression_targeted$estimate[1]*100, 1)), 'anyrepression_targeted_elected.tex')
#write(format(round(anyrepression_targeted$estimate[2]*100, 1)), 'anyrepression_targeted_unelected.tex')

#write(format(round(physicalcoercion_targeted$estimate[1]*100, 1)), 'physicalcoercion_targeted_elected.tex')
#write(format(round(physicalcoercion_targeted$estimate[2]*100, 1)), 'physicalcoercion_targeted_unelected.tex')
#write(format(round(physicalcoercion_targeted$p.value, 3)), 'pval_physicalcoercion_targeted')

contreras_overall<-t.test(contreras$overall[contreras$important==1], contreras$overall[contreras$important==0])
#write(format(round(contreras_overall$estimate[1]*100, 2)), 'contreras_overall_important.tex')
#write(format(round(contreras_overall$estimate[2]*100, 2)), 'contreras_overall_unimportant.tex')
#write(format(round(contreras_overall$p.value, 2)), 'contreras_overall_pval.tex')

contreras_physicalcoercion<-t.test(contreras$victim[contreras$important==1], contreras$victim[contreras$important==0])
#write(format(round(contreras_physicalcoercion$estimate[1]*100, 2)), 'contreras_physicalcoercion_important.tex')
#write(format(round(contreras_physicalcoercion$estimate[2]*100, 2)), 'contreras_physicalcoercion_unimportant.tex')
#write(format(round(contreras_physicalcoercion$p.value, 2)), 'contreras_physicalcoercion_pval.tex')


